import Nav from '../../src/components/common/nav/nav.vue'
import {ApiUrl,Islogined,returnId,show,hide} from './ApiUrl.js'
import loading from '../../src/components/common/loading/loading.vue'
import fangerror from '../../src/components/common/fangerror/fangerror.vue'
import Swiper from '../plugins/swiper4x/swiper.min'
require('./corAPI');
import {timerScale} from '../plugins/scale/self_adaption_scale'
import {backgroundData} from '../plugins/mui/mater.data'
require("./lib/select2/js/select2.min");
export default {
  components: {
    loading,fangerror,
  },
  data(){
    return {
      FangLoading:false,//Loading控制，true显示，false不显示
      fangerroring:false,//提示文本弹窗，true显示，false不显示
      fangerrorText:'',//提示文本弹窗内容
      listTitle:'',//指标类型文本
      userName:'',//用户名
      htmlTitle:'指标设置',
      settingList:[],
      dataLength:[],
      swiperData:[  //swiper数据
        {name:'建设类',industryCode:'03'},
        {name:'运营类',industryCode:'02'},
        {name:'房产类',industryCode:'01'},
      ],
      indexType:null,     //定义全局参数
      specialtyCode:null,
      industryCode:null,
      page:1,
      swiperIndex:''  //swiper下标
    }
  },
  created: function() {

  },
  mounted(){
    let _This = this;
    _This.industryCode = this.$route.query.industryCode;   //获取首页传过来的参数
    _This.specialtyCode = this.$route.query.specialtyCode;   //获取首页传过来的参数
    _This.indexType = this.$route.query.indexType;   //获取首页传过来的参数
    this.userName = localStorage.getItem('userName');
    let FangUpadateIndexSorting = localStorage.getItem('FangUpadateIndexSorting');
    let GetQuerybyLIndustryCode = '';
    if(FangUpadateIndexSorting != '' && FangUpadateIndexSorting != null && FangUpadateIndexSorting != undefined){
      GetQuerybyLIndustryCode = localStorage.getItem('FangUpadateIndexSorting');
    }else {
      GetQuerybyLIndustryCode = '03'
    }
    let params = {
      industryCode:GetQuerybyLIndustryCode,
      // specialtyCode:_This.specialtyCode,
      indexType:3,
      parent:true,
      subStatus:true,
      page:1,
      pageSize:10
    };
    this.getSettingList(params);
    timerScale();
    $('.select2-dropdown').remove();
    $('select').select2({
      language: "zh-CN",
    });
    this.getSwiperData();
    for(let i=0;i<_This.swiperData.length;i++){
      if( _This.industryCode == _This.swiperData[i].industryCode){
        _This.listTitle = _This.swiperData[i].name;    //初始化分类名称
        _This.swiperIndex = i;
      }
    }
   /* let windowH = $(window).height();
    let meunBoxH = $('.meunBox').outerHeight(true);
    let megessBoxH = $('.megessBox').outerHeight(true);
    $('.contentLeft,.contentRight').height( windowH - meunBoxH - megessBoxH );
    $('.tabContentBox').height(windowH - meunBoxH);*/
  },
  methods:{
    setTimeoutError:function () {
      setTimeout(() => {
        $('.Fang_errorBox').hide()
        this.fangerroring = false
      }, 2000)
    },
    returnUp: function(){
      this.$router.back(-1);
    },
    BindTapMaterPicker:function () {
      var materPicker = new mui.PopPicker();
      materPicker.setData(backgroundData());
      materPicker.show(function(items) {  //弹窗回调
        //返回 false 可以阻止选择框的关闭
        //return false;
      });
    },
    //更换指标分类选中效果
    /*
    * {name:'建设类',status:true,industryCode:'03'},
    * */
    BindUpadateIndexSorting:function(name,industryCode,e){
      let _This = this;
      let eT = e.currentTarget;
      let childDom = eT.parentNode.childNodes;
      _This.industryCode = industryCode;
      _This.page = 1;  //每次切换，page都回到初始值
      // console.log(e.currentTarget);
/*      let ObjectSetItem = {name,status,industryCode};
      JSON.stringify(ObjectSetItem)*/
      let params = {
        industryCode:industryCode,
        // specialtyCode:_This.specialtyCode,
        indexType:3,
        parent:true,
        subStatus:true,
        page:_This.page,
        pageSize:10
      };
      // this.getSettingList(params);
      _This.settingListAjax(params,function (data) {
        if(data == '' || data == null){   //没有数据时不添加active类名
          _This.fangerroring = true;  //提示
          _This.fangerrorText = name+'暂无关注的指标';
          _This.setTimeoutError();
        }else {
          localStorage.setItem('FangUpadateIndexSorting',industryCode);
          _This.settingList = data;
          for(let i=0;i<childDom.length;i++){
            childDom[i].classList.remove('active')  //先删除所有含有active 类名
          }
          eT.classList.add('active');  //单独添加active 类名
          _This.$refs.indexsublist.finishInfinite(false);  //重启上拉加载
          _This.listTitle = name;
        }
      });
    },
    getSwiperData:function () {
      let _This = this;
      setTimeout(() => {
        /*swiper配置js*/
        var swiper = new Swiper('.swiper-container-major', {
          slidesPerView: 3,
          spaceBetween: 10,
          initialSlide:_This.swiperIndex,   //下标
          pagination: {
            el: '.swiper-pagination',
            clickable: true,
          },
        });
      },200)
    },
    /*chooseNormStatus:function (item) {   //不能操作
      let _This = this;
      item.subStatus = !item.subStatus;   //选中样式
      let params = {
        specialtyCode:item.specialtyCode,
        indexType:item.indexType,
        indexCode:item.indexCode
      };
      // console.log(params);
      if(item.subStatus == true){
        _This.saveSettingSub(params,function () {  //发送请求
            mui.confirm('关注成功','提示',['确定'],function (e) {   //提示弹窗

            })
        })
      }else {
        _This.saveSettingSub(params,function () {  //发送请求
          mui.confirm('已取消关注','提示',['确定'],function (e) {   //提示弹窗

          })
        })
      }
    },*/
    getSettingList:function (SettingListObj) {
      let _This = this;
      _This.settingListAjax(SettingListObj,function (data) {
        _This.settingList = data;
      })
    },
    saveToIndex:function () {   //保存按钮
      let _This = this;
      alert('点错了')
      // let settingSubList = [];
      // let params = {
      //   specialtyCode:_This.specialtyCode,
      //   indexType:_This.indexType,
      // };
      // let $icon = $('#chooseTargetList li .icon-gou');
      // $icon.each(function (j,dom) {   //遍历打钩按钮
      //   if($(dom).hasClass('cur')){
      //     settingSubList.push({
      //       indexType:$(dom).attr('data-indextype'),
      //       indexCode:$(dom).attr('data-indexCode'),
      //       indexName:$(dom).attr('data-indexName'),
      //     })
      //   }
      // });
      // for(let i = 0;i < settingSubList.length;i++){
      //   params["indexSubList["+ i + "].indexType"] = settingSubList[i].indexType;
      //   params["indexSubList["+ i + "].indexCode"] = settingSubList[i].indexCode;
      //   params["indexSubList["+ i + "].indexName"] = settingSubList[i].indexName;
      // }
      // console.log(params);
      //保存值回调
      // _This.saveSettingSub(params,function () {
      //   mui.confirm('保存成功','提示',['返回首页','拒绝返回'],function (e) {   //提示弹窗
      //     if(e.index == 0){
      //       _This.$router.push({path:'index'});
      //     }else {
      //       return;
      //     }
      //   })
      // })
    },
    //上拉加载更多
    infinite(done) {
      let _This = this;
      if(_This.noData) {
        setTimeout(() => {
          _This.$refs.indexsublist.finishInfinite(2);
        });
        return;
      }
      setTimeout(() => {
        _This.$nextTick(()=>{
          if(_This.dataLength > 10){
            _This.page =  _This.page + 1;
            let params = {
              industryCode:_This.industryCode,
              // specialtyCode:_This.specialtyCode,
              indexType:3,
              parent:true,
              subStatus:true,
              page:_This.page,
              pageSize:10
            };
            _This.settingListAjax(params,function (data) {   //上拉时发送请求
              if(data=='' || data == null){
                _This.fangerroring = true;  //提示
                _This.fangerrorText = '没有更多数据了';
                _This.setTimeoutError();
                done(true);//没有更多数据
              }else {
                done();
              }
              if(_This.settingList == ''){
                _This.settingList = data;
              }else {
                for(let key in data){
                  _This.settingList.push(data[key]);
                }
              }
            });
          }
        })
      }, 500)
      //回调选中数据 specialtyCode,indexType,parent,page,pageSize,fn

    },
    settingListAjax:function (params,fn) {
      let _This = this;
      _This.FnAxiosAjax('post','/api/indexcategory/querybyspecialtycode',params,function (data) {
        !! fn && fn(data.results);
        _This.dataLength = data.totalCount;  //数据总条数
      })
    },
    saveSettingSub:function (params,fn) {
      this.FnAxiosAjax('post','/api/indexcategory/updateSubStatus',params,function (data) {
        !! fn && fn(data.results);
      })
    }
  }

}
